package com.lishe.lishe.lishe.module.shopingcart.shoppingcartpresenter;

import com.lishe.lishe.lishe.module.shopingcart.bean.ShoppingCartObject;
import com.lishe.lishe.lishe.module.shopingcart.shoppcarinterface.ShoppingCartInterface;
import com.lishe.lishe.retrofit.BaseRequestBody;
import com.lishe.lishe.retrofit.DataResponseListener;
import com.lishe.lishe.retrofit.RetrofitFactory;

import io.reactivex.Observer;
import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.disposables.Disposable;
import io.reactivex.schedulers.Schedulers;

/**
 * Created by shino on 2017/12/28.
 */

public class ShoppingCartPresenter {
    private static ShoppingCartPresenter shoppingCartPresenter;

    public static ShoppingCartPresenter getInstance() {
        if (shoppingCartPresenter == null) {
            shoppingCartPresenter = new ShoppingCartPresenter();
        }

        return shoppingCartPresenter;
    }

    /**
     * 获取购物车列表
     */
    public void getCartList(String token, final DataResponseListener<ShoppingCartObject> listener) {
        ShoppingCartInterface shoppingCartInterface = RetrofitFactory.create().create(ShoppingCartInterface.class);
        shoppingCartInterface.getCartList(token).subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new Observer<BaseRequestBody<ShoppingCartObject>>() {
                    @Override
                    public void onSubscribe(Disposable d) {

                    }

                    @Override
                    public void onNext(BaseRequestBody<ShoppingCartObject> baseShoppingCartDataBaseRequestBody) {
                        if (baseShoppingCartDataBaseRequestBody.getResult() == 100 && baseShoppingCartDataBaseRequestBody.getErrcode() == 0) {
                            listener.onSuccess(baseShoppingCartDataBaseRequestBody.getData());
                        } else {
                            listener.onFail(baseShoppingCartDataBaseRequestBody.getMsg());
                        }
                    }

                    @Override
                    public void onError(Throwable e) {
                        listener.onError(e);
                    }

                    @Override
                    public void onComplete() {

                    }
                });
    }
}
